<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
<!--    <el-form-item label="图书编号" prop="booknum">-->
<!--      <el-input v-model="dataForm.booknum" placeholder="图书编号"></el-input>-->
<!--    </el-form-item>-->
    <el-form-item label="图书名称" prop="bookname">
      <el-input v-model="dataForm.bookname" placeholder="图书名称"></el-input>
    </el-form-item>
      <el-form-item label="图书图片" >
        <el-upload
          class="avatar-uploader"
          :action="url"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          name="file">
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
    <el-form-item label="图书作者" prop="bookauthor">
      <el-input v-model="dataForm.bookauthor" placeholder="图书作者"></el-input>
    </el-form-item>
<!--    <el-form-item label="图书种类id" prop="bookcateid">-->
<!--      <el-input v-model="dataForm.bookcateid" placeholder="图书种类id"></el-input>-->
<!--    </el-form-item>-->
      <el-form-item label="图书种类" prop="departId">
        <el-select v-model="dataForm.bookcateid" placeholder="请选择">
          <el-option v-for="(item,i) in bookCate" :label="item.catetypename" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
    <el-form-item label="图书出版社" prop="bookpublish">
      <el-input v-model="dataForm.bookpublish" placeholder="图书出版社"></el-input>
    </el-form-item>
    <el-form-item label="图书库存" prop="bookstock">
      <el-input v-model="dataForm.bookstock" placeholder="图书库存"></el-input>
    </el-form-item>
    <el-form-item label="图书描述" prop="bookdescribe">
      <el-input v-model="dataForm.bookdescribe" placeholder="图书描述"></el-input>
    </el-form-item>
<!--    <el-form-item label="图书图片" prop="bookphoto">-->
<!--      <el-input v-model="dataForm.bookphoto" placeholder="图书图片"></el-input>-->
<!--    </el-form-item>-->
    <el-form-item label="图书摆放位置" prop="booklocation">
      <el-input v-model="dataForm.booklocation" placeholder="图书摆放位置"></el-input>
    </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    created () {
      this.getBookcatename()
    },
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          booknum: '',
          bookname: '',
          bookauthor: '',
          bookcateid: '',
          bookpublish: '',
          bookstock: '',
          bookdescribe: '',
          bookphoto: '',
          booklocation: ''
        },
        dataRule: {
          booknum: [
            { required: true, message: '图书编号不能为空', trigger: 'blur' }
          ],
          bookname: [
            { required: true, message: '图书名称不能为空', trigger: 'blur' }
          ],
          bookauthor: [
            { required: true, message: '图书作者不能为空', trigger: 'blur' }
          ],
          bookcateid: [
            { required: true, message: '图书种类id不能为空', trigger: 'blur' }
          ],
          bookpublish: [
            { required: true, message: '图书出版社不能为空', trigger: 'blur' }
          ],
          bookstock: [
            { required: true, message: '图书库存不能为空', trigger: 'blur' }
          ],
          bookdescribe: [
            { required: true, message: '图书描述不能为空', trigger: 'blur' }
          ],
          // bookphoto: [
          //   { required: true, message: '图书图片不能为空', trigger: 'blur' }
          // ],
          booklocation: [
            { required: true, message: '图书摆放位置不能为空', trigger: 'blur' }
          ]
        },
        bookCate: {
          id: '',
          catetypename: ''
        },
        url: '',
        imageUrl: ''
      }
    },
    methods: {
      init (id) {
        this.url = this.$http.adornUrl(`/bs_book/bsbook/bookUpload?token=${this.$cookie.get('token')}`)
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/bs_book/bsbook/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.booknum = data.bsBook.booknum
                this.dataForm.bookname = data.bsBook.bookname
                this.dataForm.bookauthor = data.bsBook.bookauthor
                this.dataForm.bookcateid = data.bsBook.bookcateid
                this.dataForm.bookpublish = data.bsBook.bookpublish
                this.dataForm.bookstock = data.bsBook.bookstock
                this.dataForm.bookdescribe = data.bsBook.bookdescribe
                this.dataForm.bookphoto = data.bsBook.bookphoto
                this.dataForm.booklocation = data.bsBook.booklocation
                if (id > 0) {
                  this.imageUrl = data.bsBook.bookphoto
                }
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bs_book/bsbook/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'booknum': this.dataForm.booknum,
                'bookname': this.dataForm.bookname,
                'bookauthor': this.dataForm.bookauthor,
                'bookcateid': this.dataForm.bookcateid,
                'bookpublish': this.dataForm.bookpublish,
                'bookstock': this.dataForm.bookstock,
                'bookdescribe': this.dataForm.bookdescribe,
                'bookphoto': this.dataForm.bookphoto,
                'booklocation': this.dataForm.booklocation
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      getBookcatename () {
        this.$http({
          url: this.$http.adornUrl(`/bs_book/bsbook/getBookcatename`),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.bookCate = data.date
            console.log(data.date)
          }
        })
      },
      handleAvatarSuccess (res, file) {
        console.log(file)
        this.imageUrl = URL.createObjectURL(file.raw)
        this.dataForm.bookphoto = res.msg
        console.log(this.imageUrl)
      },
      beforeAvatarUpload (file) {
        console.log(file)
        const isJPG = file.type === 'image/jpeg'
        const isLt2M = file.size / 1024 / 1024 < 2
        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!')
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!')
        }
        return isJPG && isLt2M
      }
    }
  }
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
